﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraCharts;

namespace DrugsShopManagement.DBCLASS
{
    class ChartStatisticsPreviewAndPrintHelper
    {
        /// <summary>
        /// 设置固定宽高适应A4页面
        /// </summary>
        /// <param name="chart"></param>
        private void SetChartHeightAndWidth(ChartControl chart)
        {
            chart.Width = 624;
            chart.Height = 350;
        }

        /// <summary>
        /// 设置与chart等比例宽高适应A4页面
        /// </summary>
        /// <param name="chart"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        private void SetChartHeightAndWidth(ChartControl chart,int width,int height)
        {
            float a=Convert.ToInt32(Convert.ToString(width));
            float b = Convert.ToInt32(Convert.ToString(height));
            
            chart.Width = 624;
            chart.Height = Convert.ToInt32(b/a* 624);
        }

        /// <summary>
        /// 固定宽高打印预览
        /// </summary>
        /// <param name="chart">ChartControl</param>
        public void ShowChartPreview(ChartControl chart)
        {
            // 检查ChartControl是否可预览
            if (!chart.IsPrintingAvailable)
            {
                MessageBox.Show("找不到组件：DevExpress.XtraPrinting.v10.2.4.dll", "错误");
                return;
            }

            //设置停靠模式，以使接下来的设置宽高生效
            chart.Dock = DockStyle.None;
            //设置固定宽高适应A4页面
            SetChartHeightAndWidth(chart);
            //打开打印预览
            chart.ShowPrintPreview();
            //设置回原来的停靠模式
            chart.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// 自适应宽高A4页面打印预览
        /// </summary>
        /// <param name="chart">ChartControl</param>
        /// <param name="width">宽</param>
        /// <param name="height">高</param>
        public void ShowChartPreview(ChartControl chart, int width, int height)
        {
            // 检查ChartControl是否可预览
            if (!chart.IsPrintingAvailable)
            {
                MessageBox.Show("找不到组件：DevExpress.XtraPrinting.v10.2.4.dll", "错误");
                return;
            }

            //设置停靠模式，以使接下来的设置宽高生效
            chart.Dock = DockStyle.None;
            // 设置等比例宽高适应A4页面
            SetChartHeightAndWidth(chart,width,height);
            //打开打印预览
            chart.ShowPrintPreview();
            //设置回原来的停靠模式
            chart.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// 固定宽高打印预览
        /// </summary>
        /// <param name="chart">ChartControl</param>
        public void PrintChart(ChartControl chart)
        {
            try
            {
                // 检查chartControl是否可打印
                if (!chart.IsPrintingAvailable)
                {
                    MessageBox.Show("找不到组件：DevExpress.XtraPrinting.v10.2.4.dll", "错误");
                    return;
                }
                //设置停靠模式，以使接下来的设置宽高生效
                chart.Dock = DockStyle.None;
                // 设置宽高适应A4页面
                SetChartHeightAndWidth(chart);
                // 打印
                chart.Print();
                //设置回原来的停靠模式
                chart.Dock = DockStyle.Fill;
            }
            catch (Exception)
            {
                //设置回原来的停靠模式
                chart.Dock = DockStyle.Fill;
                MessageBox.Show("打印已取消！");
                //MessageBox.Show("未完成打印，错误原因：" + e.ToString());
                //chart.Dock = System.Windows.Forms.DockStyle.Fill;
            }
        }

        /// <summary>
        /// 自适应宽高A4页面打印
        /// </summary>
        /// <param name="chart"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public void PrintChart(ChartControl chart,int width,int height)
        {
            try
            {
                // 检查chartControl是否可打印
                if (!chart.IsPrintingAvailable)
                {
                    MessageBox.Show("找不到组件：DevExpress.XtraPrinting.v10.2.4.dll", "错误");
                    return;
                }
                //设置停靠模式，以使接下来的设置宽高生效
                chart.Dock = DockStyle.None;
                // 设置自适应宽高适应A4页面
                SetChartHeightAndWidth(chart,width,height);
                // 打印
                chart.Print();
                //设置回原来的停靠模式
                chart.Dock = DockStyle.Fill;
            }
            catch (Exception)
            {
                //设置回原来的停靠模式
                chart.Dock = DockStyle.Fill;
                MessageBox.Show("打印已取消！");
                //MessageBox.Show("未完成打印，错误原因：" + e.ToString());
                //chart.Dock = System.Windows.Forms.DockStyle.Fill;
            }
        }

        /// <summary>
        /// 比较两个DateTimePicker的时间，若开始时间大于结束时间则返回
        /// </summary>
        /// <param name="p1">DateTimePicker1</param>
        /// <param name="p2">DateTimePicker2</param>
        public void CompareTime(DateTimePicker p1, DateTimePicker p2)
        {
            if (p1.Value.CompareTo(p2.Value) == 1)
            {
                MessageBox.Show("开始时间不能大于结束时间！");
                return;
            }
        }
    }
}
